package com.hawthorn.admin.service.sysbizconf.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.feign.redisService.RedisBizConfFeignService;
import com.hawthorn.admin.repository.SysBizConfMapper;
import com.hawthorn.admin.service.sysbizconf.SysBizConfService;
import com.hawthorn.component.utils.bean.Map2ObjUtil;
import com.hawthorn.entity.po.SysBizConfPO;
import com.hawthorn.platform.exception.BizCode;
import com.hawthorn.platform.service.ExcelApiService;
import com.hawthorn.platform.service.TransResultService;
import com.hawthorn.platform.utils.common.ArrayMyUtil;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.iassert.AssertMyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 业务参数配置表 serviceImp 实现类</p>
 * @author: wcz
 * @created: 2022-07-01
 * @version v1.0.1
 */
@Service
public class SysBizConfServiceImpl implements SysBizConfService
{
  @Resource
  private SysBizConfMapper sysBizConfMapper;

  @Resource
  private TransResultService transResultService;

  @Resource
  private ExcelApiService excelApiService;

  @Resource
  private RedisBizConfFeignService redisBizConfFeignService;

  private HashMap<String, String> getTransHash()
  {
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("bizType", "dic|ywlx");
    transHash.put("status", "dic|yxzt");

    return transHash;
  }

  /**
   * <p>todo 查询参数信息</p >
   * @param page 分页
   * @param conditions  * @return : com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>
   * @author: wcz
   * @date: 2022/7/1 14:50
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/1 wcz v1.0.1 init
   */
  public IPage<Map<String, Object>> findBizConf(Page<Map<String, Object>> page, String conditions)
  {
    // 设置排序
    OrderItem oi = new OrderItem();
    oi.setAsc(true);
    oi.setColumn("type");
    List<OrderItem> orders = page.getOrders();
    orders.add(oi);
    page.setOrders(orders);
    return transResultService.transResult(sysBizConfMapper.findBizConf(page, conditions), getTransHash());
  }

  /**
   * <p>todo 新增</p >
   * @param param  * @return : java.util.Map<java.lang.String,java.lang.Object>
   * @author: wcz
   * @date: 2022/7/4 11:34
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 wcz v1.0.1 init
   */
  public Map<String, Object> saveBizConf(Map<String, Object> param)
  {
    SysBizConfPO sysBizConfPO = Map2ObjUtil.toObject(param, SysBizConfPO.class);
    AssertMyUtil.notNull(sysBizConfPO, BizCode.VARIABLE_NOT_NULL, "sysBizConfPO");
    //重名校验
    Integer bizConfIsExist = sysBizConfMapper.checkBizConf(sysBizConfPO.getParaKey());
    if (bizConfIsExist != 0)
    {
      AssertMyUtil.errMsg("参数代码重复");
    }
    sysBizConfMapper.save(sysBizConfPO);

    // todo: 同步到redis
    redisBizConfFeignService.reloadBizConfById(sysBizConfPO);

    return transResultService.transResult(sysBizConfPO.toMaps(), getTransHash());
  }

  /**
   * <p>todo 更新方法</p >
   * @param id 参数id
   * @param param  * @return : java.util.Map<java.lang.String,java.lang.Object>
   * @author: wcz
   * @date: 2022/7/4 16:37
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 wcz v1.0.1 init
   */
  public Map<String, Object> updateBizConf(Long id, Map<String, Object> param)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");
    SysBizConfPO sysBizConfPO = sysBizConfMapper.selectById(id);
    Map2ObjUtil.toObject(param, sysBizConfPO);
    sysBizConfMapper.updateById(sysBizConfPO);
    // todo: 同步到redis
    redisBizConfFeignService.reloadBizConfById(sysBizConfPO);

    return transResultService.transResult(sysBizConfPO.toMaps(), getTransHash());
  }

  /**
   * <p>todo 导出</p >
   * @param response 导出数据
   * @param fileName 文件名
   * @param params  * @return : void
   * @author: wcz
   * @date: 2022/7/4 16:38
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 wcz v1.0.1 init
   */
  public void export(HttpServletResponse response, String fileName, Map<String, Object> params)
  {
    String header = MyUtil.getValue(params.get("header"));
    String param = MyUtil.getValue(params.get("params"));
    Page<Map<String, Object>> page = new Page<>();
    String qc = MyUtil.getConditionsWhere(param, page);
    List<List<String>> headList = new ArrayList<>();
    List<String> headFields = excelApiService.toHeader(header, headList);
    String fields = ArrayMyUtil.join(headFields.toArray(), ",");
    List<Map<String, Object>> dataList = sysBizConfMapper.export(fields, qc);
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("biz_type", "dic|ywlx");
    excelApiService.export(response, fileName, headFields, headList, dataList, transHash);
  }

  /**
   * <p>todo 删除数据</p >
   * @param id  * @return : java.lang.Integer
   * @author: wcz
   * @date: 2022/7/4 16:38
   * @version: 1.0.1
   * Modification History:
   * Date Author Version Description
   * -----------------------------------------------------------
   * 2022/7/4 wcz v1.0.1 init
   */
  @Override
  public Integer removeBizConf(Long id)
  {
    AssertMyUtil.notNull(id, BizCode.METHOD_ARGS_NOTNULL, "id");
    // todo: 同步到redis
    // redisBizConfFeignService.delBizConf(id);

    return sysBizConfMapper.deleteById(id);
  }
}
